package LawsonTransformJava.main;
import java.io.*;

import LawsonTransformJava.pgm.PR560Control;

public class PR560Main extends ClassMain {

	/**
	 * @author edavis
	 * @param args - All parameters are <b>Required</b>
	 * <div>
	 * [0] - Payroll Year <br>
	 * [1] - Allow Blank translations
	 * </div>
	 */
	
	private static String envWhereClause = System.getenv("WHERECLAUSE");
	private static File outFile = null;
	private static File errFile = null;
	private static File convRpt = null;
	
	public static void main(String[] args) {
		Thread thread1 = null;
		//Make sure 2 parameters are passed into the Main program
		System.out.println("CLASSPATH=" + System.getenv("CLASSPATH"));
		System.out.println("WHERECLAUSE=" + System.getenv("WHERECLAUSE"));
		if (args.length!=3) {   
			usage();	//Show usage and die
		}
		
		if (args[2].equals("")) {
			outFile = new File(OUTPUTLOCATION + "/HSTLOADCSV_" + getDate() + "_" + Integer.parseInt(args[0].trim()));
			errFile = new File(OUTPUTLOCATION + "/HSTERRORCSV_" + getDate() + "_" + Integer.parseInt(args[0].trim()));
			convRpt = new File(OUTPUTLOCATION + "/HST_PR560TransformationReport_" + getDate() + "_" + Integer.parseInt(args[0].trim()) + ".txt");
		} else {
			outFile = new File(OUTPUTLOCATION + "/HSTLOADCSV_" + getDate() + "_" + Integer.parseInt(args[0].trim()) + "_" + args[2].trim());
			errFile = new File(OUTPUTLOCATION + "/HSTERRORCSV_" + getDate() + "_" + Integer.parseInt(args[0].trim()) + "_" + args[2].trim());
			convRpt = new File(OUTPUTLOCATION + "/HST_PR560TransformationReport_" + getDate() + "_" + Integer.parseInt(args[0].trim()) + "_" + args[2].trim() + ".txt");
		}

		try {
			//Create the file if it does not already exist
			outFile.createNewFile();
			PR560Control pr560ctl = null;
			
			//Output Payroll History records
			//Allow Blank translations where appropriate
			if (args[1].equalsIgnoreCase("true")) {
				pr560ctl = new PR560Control(outFile, errFile, convRpt, Integer.parseInt(args[0].trim()), true);
			}
			else if (args[1].equalsIgnoreCase("false")) {
				pr560ctl = new PR560Control(outFile, errFile, convRpt, Integer.parseInt(args[0].trim()), false);
			}
			
			if (pr560ctl!=null) {
				//Set Where Clause from environment if found
				if (envWhereClause!=null)
					if (!envWhereClause.equals(""))
						pr560ctl.setWhereClause(envWhereClause);
				
				thread1 = new Thread(pr560ctl);
				thread1.start();
			}
			else
				usage();
		} catch (NumberFormatException nfe) {
			usage();
		} catch (IOException ioe) {
			System.out.println("IOException: " + ioe);
		}
	}
	
	private static void usage() {
		System.err.println("Usage - PR560Main payrollyear=CCYY true|false filesuffix");
		System.exit(1);
	}
}
